//
//  ViewController.swift
//  yimiao
//
//  Created by Qingke on 16/3/30.
//  Copyright © 2016年 Qingke. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    
    @IBOutlet weak var stateLabel: UILabel!
    @IBOutlet weak var statelView: UILabel!
    @IBOutlet weak var buttonlabel: UIButton!
    
    @IBOutlet weak var viewImBackground: UIImageView!
    @IBOutlet weak var fanRenTimes: UILabel!
    @IBOutlet weak var daShenTimes: UILabel!
    @IBOutlet weak var totalTimesLabel: UILabel!
    
    
    var downTime = NSDate()
    var totalTimesN = 0
    var fanRenTimesN = 0 {
        didSet{
            fanRenTimes.text = String( fanRenTimesN)
            totalTimesN++
            totalTimesLabel.text = String(totalTimesN)
            
        }
    }
    var daShenTimesN = 0 {
        didSet{
            totalTimesN++
            daShenTimes.text = String( daShenTimesN)
            totalTimesLabel.text = String(totalTimesN)
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
        let button_highlight = UIImage( named: "指纹R")
        buttonlabel.setImage(button_highlight, forState: .Normal)
    }

    @IBAction func buttonDown(sender: UIButton) {
        stateLabel.text = "站住一秒哦"
        downTime = NSDate()
        viewImBackground.image = UIImage ( named: "default_bg")
    }

    @IBAction func buttonUp(sender: UIButton) {
        
        let intervalTime = abs ( downTime.timeIntervalSinceNow )
        stateLabel.text = "\(intervalTime)"
        print(intervalTime)
        switchTime(intervalTime)
    }
    
    func switchTime ( intervalTime: Double) {
        let rank = abs(1 - intervalTime)
        if rank > 0.1 {
            fanRenTimesN++
        } else {
            daShenTimesN++
            change()
        }
    }
    
    func change() {
        viewImBackground.image = UIImage( named: "cool_bg")
    }
}

